import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:luckdragon/api/apis.dart';
import 'package:luckdragon/infoPage.dart';
import 'package:luckdragon/settingPage.dart';
import 'package:luckdragon/sshPage.dart';
import 'package:expansion_tile_card/expansion_tile_card.dart';

class serverControl extends StatefulWidget {
  final String ip;
  final String port;
  final String remarks;
  serverControl({
    Key? key,
    this.ip = "",
    this.port = "",
    this.remarks = "",
  }) : super(key: key);
  @override
  _serverControlState createState() => _serverControlState();
}

class _serverControlState extends State<serverControl> {
  String _cpu = "0";
  String _memory = "0";
  String _netUP = "0";
  String _netDown = "0";
  IconData _systemIcon = FontAwesomeIcons.windows;

  @override
  Widget build(BuildContext context) {
    const timeout = const Duration(seconds: 4);
    Timer(timeout, () async {
      try {
        await Apis.getInfo(widget.ip, widget.port).then((r) {
          setState(() {
            String json = r.data!;
            Map<String, dynamic> info = jsonDecode(json);
            if (info["system"]["system"].toString() == "Windows") {
              _systemIcon = FontAwesomeIcons.windows;
            }
            if (info["system"]["system"].toString() == "Linux") {
              _systemIcon = FontAwesomeIcons.linux;
            } else {
              _systemIcon = FontAwesomeIcons.angry;
            }
            _cpu = info["cpu"]["cpu_percent"].toString();
            _memory = info["memory"]["percent"].toString();
            _netUP = (info["net"]["bytes_sent"] / 128).toStringAsFixed(2);
            _netDown = (info["net"]["bytes_rcvd"] / 128).toStringAsFixed(2);
          });
        });
      } catch (e) {}
    });

    return ExpansionTileCard(
      expandedTextColor: Colors.red,
      leading: CircleAvatar(
          child: Icon(
        _systemIcon,
      )),
      title: Text(
        widget.ip,
        style: TextStyle(
          fontSize: 20,
        ),
      ),
      subtitle: Text(widget.remarks),
      children: [
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Divider(
              height: 20,
              thickness: 0.5,
              indent: 30,
              endIndent: 60,
            ),
            Padding(
              padding: EdgeInsets.only(left: 30),
              child: Row(
                children: [
                  Icon(
                    FontAwesomeIcons.microchip,
                    size: 20,
                  ),
                  const SizedBox(
                    width: 20,
                  ),
                  Text(
                    _cpu,
                    style: TextStyle(fontSize: 15),
                  ),
                  Text(
                    '%',
                    style: TextStyle(fontSize: 10),
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Padding(
              padding: EdgeInsets.only(left: 30),
              child: Row(
                children: [
                  Icon(
                    FontAwesomeIcons.memory,
                    size: 20,
                  ),
                  const SizedBox(
                    width: 20,
                  ),
                  Row(
                    children: [
                      Text(
                        _memory,
                        style: TextStyle(fontSize: 15),
                      ),
                      Text(
                        '%',
                        style: TextStyle(fontSize: 10),
                      )
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Padding(
              padding: EdgeInsets.only(left: 30),
              child: Row(
                children: [
                  Icon(
                    FontAwesomeIcons.networkWired,
                    size: 20,
                  ),
                  const SizedBox(
                    width: 20,
                  ),
                  Row(
                    children: [
                      Icon(
                        FontAwesomeIcons.angleDoubleUp,
                        size: 10.0,
                      ),
                      Padding(
                        padding: EdgeInsets.only(right: 2, left: 2),
                        child: Row(
                          children: [
                            Text(
                              _netUP,
                              style: TextStyle(fontSize: 15),
                            ),
                            Text(
                              'kb/s',
                              style: TextStyle(fontSize: 10),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  Row(
                    children: [
                      Icon(
                        FontAwesomeIcons.angleDoubleDown,
                        size: 10.0,
                      ),
                      Padding(
                        padding: EdgeInsets.only(right: 2, left: 2),
                        child: Row(
                          children: [
                            Text(
                              _netDown,
                              style: TextStyle(fontSize: 15),
                            ),
                            Text(
                              'kb/s',
                              style: TextStyle(fontSize: 10),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            Padding(
              padding: EdgeInsets.only(top: 0),
              child: ButtonBar(
                alignment: MainAxisAlignment.spaceAround,
                buttonHeight: 52.0,
                buttonMinWidth: 90.0,
                children: <Widget>[
                  TextButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => settingPage(
                                  title: widget.ip,
                                  ip: widget.ip,
                                  port: widget.port,
                                )),
                      );
                    },
                    child: Column(
                      children: [
                        Icon(
                          Icons.settings,
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 2.0),
                        ),
                        const Text(
                          'Setting',
                          style: TextStyle(
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(width: 8),
                  TextButton(
                    child: Column(
                      children: [
                        Icon(
                          Icons.laptop,
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 2.0),
                        ),
                        const Text(
                          'Terminal',
                        ),
                      ],
                    ),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => sshPage(
                                  title: widget.ip,
                                )),
                      );
                    },
                  ),
                  const SizedBox(width: 8),
                  TextButton(
                    child: Column(
                      children: [
                        Icon(
                          Icons.toc,
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(vertical: 2.0),
                        ),
                        const Text(
                          'Process',
                        ),
                      ],
                    ),
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => infoPage(
                                  title: widget.ip,
                                  ip: widget.ip,
                                  port: widget.port,
                                )),
                      );
                    },
                  ),
                  const SizedBox(width: 8),
                ],
              ),
            ),
          ],
        ),
      ],
    );
  }
}
